<template>
    <div>
        <el-card>
            <el-table border style="margin: 10px 0;" :data="skuArr">
                <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
                <el-table-column label="名称" show-overflow-tooltip width="150px" prop="skuName"></el-table-column>
                <el-table-column label="描述" width="150px" prop="skuDesc"></el-table-column>
                <el-table-column label="图片" width="180px">
                    <template #="{ row, $index }">
                        <img :src="row.skuDefaultImg" alt="" width="100px" height="100px">
                    </template>
                </el-table-column>
                <el-table-column label="重量" width="150px" prop="weight"></el-table-column>
                <el-table-column label="价格" width="150px" prop="price"></el-table-column>
                <el-table-column label="操作" width="250px" fixed="right">
                    <template #="{ row, $index }">
                        <el-button :icon="row.isSale ? 'Bottom' : 'Top'" size="small"
                            :type="row.isSale ? 'warning' : 'success'" @click="changeSale(row)"></el-button>
                        <el-button icon="Edit" size="small" type="primary"></el-button>
                        <el-button icon="InfoFilled" size="small" type="info" @click="showInfo(row)"></el-button>
                        <el-button icon="Delete" size="small" type="danger" @click="deleteInfo(row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7]"
                :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total"
                @size-change="getHasSku(1)" @current-change="getHasSku" />

            <el-drawer v-model="drawer" :direction="direction">
                <template #header>
                    <h4>查看商品详情</h4>
                </template>
                <template #default>
                    <el-row style="margin: 0px 2px">
                        <el-col :span="6">名称</el-col>
                        <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                    </el-row>
                    <el-row style="margin: 20px 2px">
                        <el-col :span="6">描述</el-col>
                        <el-col :span="18">{{ skuInfo.Desc }}</el-col>
                    </el-row>
                    <el-row style="margin: 20px 2px">
                        <el-col :span="6">价格</el-col>
                        <el-col :span="18">￥ {{ skuInfo.price }}</el-col>
                    </el-row>
                    <el-row style="margin: 20px 2px">
                        <el-col :span="6">平台属性</el-col>
                        <el-col :span="18">
                            <el-tag style="margin: 5px;" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{
                item.valueName }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 20px 2px">
                        <el-col :span="6">销售属性</el-col>
                        <el-col :span="18">
                            <el-tag style="margin: 5px;" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{
                item.saleAttrValueName }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 20px 2px">
                        <el-col :span="6">商品图片</el-col>
                        <el-col :span="18">
                            <el-carousel :interval="4000" type="card" height="200px">
                                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                    <img :src="item.imgUrl" width="100%" height="100%">
                                </el-carousel-item>
                            </el-carousel>
                        </el-col>
                    </el-row>
                </template>
            </el-drawer>
        </el-card>
    </div>
</template>

<script setup lang='ts'>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus';
import { reqSkuList, reqSaleSku, reqCancelSaleSku, reqSkuInfo, reqDeleteSku } from '@/api/product/sku'
const pageNo = ref(1)
const pageSize = ref(4)
const total = ref(0)
const skuArr = ref([])
const drawer = ref(false)
const skuInfo = ref({})
onMounted(() => {
    getHasSku()
})

const getHasSku = async (page = 1) => {
    pageNo.value = page
    const res = await reqSkuList(pageNo.value, pageSize.value)
    if (res.code == 200) {
        total.value = res.data.total
        skuArr.value = res.data.records
    }
}

const changeSale = async (row) => {
    if (row.isSale) {
        await reqCancelSaleSku(row.id)
        ElMessage({ type: 'success', message: '下架成功' })
        getHasSku(pageNo.value)
    } else {
        await reqSaleSku(row.id)
        ElMessage({ type: 'success', message: '上架成功' })
        getHasSku(pageNo.value)
    }
}

const showInfo = async (row) => {
    drawer.value = true
    const res = await reqSkuInfo(row.id)
    skuInfo.value = res.data
}

const deleteInfo = async (row) => {
    const res = await reqDeleteSku(row.id)
    if (res.code == 200) {
        ElMessage({ type: 'success', message: '删除成功' })
        getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
    } else {
        ElMessage({ type: 'error', message: '删除失败' })
    }
}
</script>

<style scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>